<?php

namespace app\admin\controller\wechat;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Menu extends Base
{

    /**
     * 主页面
     * @return mixed
     */
    public function index()
    {
        $list = DB::name('wx_user_tag')->select();
        array_unshift($list, ['id' => 0, 'tag_id' => 0, 'name' => '默认菜单']);
        $this->assign('tag_list', $list);
        return view();
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        unset($post['map']);
        unset($post['page']);
        unset($post['limit']);
        $post['wx_menu.tag_id'] = $post['tag_id'];
        unset($post['tag_id']);
        $data['count'] = Db::view('wx_menu', 'id,name,pid,info,msg,type,status,tag_id')
            ->view(['wx_menu' => 'p_menu'], ['name' => 'p_name'], 'wx_menu.pid=p_menu.id', 'LEFT')
            ->where($post)
            ->count();
        //exit;
        if ($data['count'] == 0) {
            $list = DB::name('wx_menu')->where('tag_id', 0)->select();
            foreach ($list as &$item) {
                unset($item['id']);
                $item['tag_id'] = $post['wx_menu.tag_id'];
            }
            DB::name('wx_menu')->insertAll($list);
        }
        //exit;
        $data['data'] = Db::view('wx_menu', 'id,name,pid,info,msg,type,status,tag_id')
            ->view(['wx_menu' => 'p_menu'], ['name' => 'p_name'], 'wx_menu.pid=p_menu.id', 'LEFT')
            ->where($post)
            // ->fetchSql(true)
            ->order('id asc')->select();
        $wx_type = config('param.wx_type');
        foreach ($data['data'] as &$item) {
            $item['type'] = $wx_type[$item['type']];
        }

        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function c_wx_menu()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $list = DB::name('wx_menu')
                ->where('status', 1)
                ->where('tag_id', $post['tag_id'])
                ->order('id asc')
                ->select();
            $menu = array();
            $pid = 0;
            foreach ($list as $item) {
                if ($item['pid'] == 0) {
                    $menu[$pid]['name'] = $item['name'];
                    $pid = $pid + 1;
                } else {
                    $tmp = array();
                    $tmp['name'] = $item['name'];
                    switch ($item['type']) {
                        case 2:
                            $tmp['type'] = 'view';
                            $tmp['url'] = $item['info'];
                            break;
                        case 3:
                            $tmp['type'] = 'click';
                            $tmp['key'] = $item['info'];
                            break;
                        case 4:
                            $tmp['type'] = 'miniprogram';
                            $tmp['url'] = 'https://jt.yooee.ltd';
                            $tmp['appid'] = $item['info'];
                            $tmp['pagepath'] = 'pages/index/index';
                            break;
                        default:
                            $tmp['type'] = 'view';
                            $tmp['url'] = 'https://jt.yooee.ltd';
                    }
                    $menu[$pid - 1]['sub_button'][] = $tmp;
                }
            }
            if ($list[0]['tag_id'] > 0) {
                $new['matchrule']['tag_id'] = $post['tag_id'];
                $url2 = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=" . cache('access_token');
            } else {
                $url2 = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . cache('access_token');
            }
            //dump($menu);
            $new['button'] = $menu;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url2);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($new, JSON_UNESCAPED_UNICODE));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $tmpInfo = curl_exec($ch);
            if (curl_errno($ch)) {
                // dump(curl_error($ch));
            }
            curl_close($ch);
            $res = json_decode($tmpInfo, true);
            if (isset($res['errcode']) && $res['errcode'] == 0) {
                $this->success('操作成功');
            }
            if (isset($res['menuid'])) {
                $this->success('操作成功');
            }
            $this->error('操作失败(' . $res['errcode'] . '|' . $res['errmsg'] . ')');
        } else {
            $this->assign('tag_id', $post['tag_id']);
            return view('');
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {

            $post = $this->request->param();


            if (Db::name('wx_menu')->update($post) !== false) {
                $this->success('操作成功');
            }
            $this->error('编辑失败');

        }

    }


    public function editcategory()
    {
        if ($this->request->isPost()) {

            $post = $this->request->param();
            if (!isset($post['name']) || empty($post['name'])) {
                $this->error(' 名称不能为空');
            }
            if ($post['pid'] == 0) {
                if ($post['type'] <> 1) {
                    $this->error(' 一级菜单必须选择菜单类型为<一级菜单>');
                }

            } else {
                if ($post['type'] == 1) {
                    $this->error(' 子菜单不能选择菜单类型为<一级菜单>');
                }
                if (empty($post['info'])) {
                    $this->error(' 子菜单中菜单内容必填');
                }
            }

            if (Db::name('wx_menu')->update($post) !== false) {
                $this->success('操作成功');
            }
            $this->error('编辑失败');

        } else {
            $info = Db::name('wx_menu')
                ->where('id', input('id'))
                ->find();
            $this->assign('info', $info);;
            $this->assign('wx_type', config('param.wx_type'));
            return view('add');
        }

    }
}